package com.novunda.platform.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.novunda.platform.common.web.controller.BaseController;
import com.novunda.platform.entities.Menu;
import com.novunda.platform.entities.base.BaseEntity;
import com.novunda.platform.services.PlatformService;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 菜单Controller
 * @author linfeng
 * @since 2016/9/1
 */
@Controller
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {


    @Autowired
    private PlatformService platformService;

    @ModelAttribute("menu")
    public Menu get(@RequestParam(required=false) Long id) {
        if (id != null){
            return platformService.getMenu(id);
        }else{
            return new Menu();
        }
    }

    @RequestMapping({"list", ""})
    public String list(Model model) {
        List<Menu> list = Lists.newArrayList();
        List<Menu> sourcelist = platformService.findAllMenu();
        Menu.sortList(list, sourcelist, 1L);
        model.addAttribute("list", list);
        return "modules/sys/menuList";
    }

//    @RequiresPermissions("sys:menu:view")
    @RequestMapping("form")
    public String form(Menu menu, Model model) {
        if (menu.getParent()==null||menu.getParent().getId()==null){
            menu.setParent(new Menu(1L));
        }
        menu.setParent(platformService.getMenu(menu.getParent().getId()));
        menu.setShortcut(BaseEntity.SHORTCUT_NO);
        menu.setIsShow(BaseEntity.SHOW);
        model.addAttribute("menu", menu);
        return "modules/sys/menuForm";
    }

//    @RequiresPermissions("sys:menu:edit")
    @RequestMapping("save")
    public String save(Menu menu, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, menu)){
            return form(menu, model);
        }
        platformService.saveMenu(menu);
        addMessage(redirectAttributes, "保存菜单'" + menu.getName() + "'成功");
        return "redirect:"+ "/sys/menu";
    }

//    @RequiresPermissions("sys:menu:edit")
    @RequestMapping("delete")
    public String delete(Long id, RedirectAttributes redirectAttributes) {
        if (Menu.isRoot(id)){
            addMessage(redirectAttributes, "删除菜单失败, 不允许删除顶级菜单或编号为空");
        }else{
            platformService.deleteMenu(id);
            addMessage(redirectAttributes, "删除菜单成功");
        }
        return "redirect:"+ "/sys/menu";
    }

    @RequestMapping(value = "updateSort")
    public String updateSort(Long[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
        int len = ids.length;
        Menu[] menus = new Menu[len];
        for (int i = 0; i < len; i++) {
            menus[i] = platformService.getMenu(ids[i]);
            menus[i].setSort(sorts[i]);
            platformService.saveMenu(menus[i]);
        }
        addMessage(redirectAttributes, "保存菜单排序成功!");
        return "redirect:/sys/menu/";
    }


    @RequiresUser
    @ResponseBody
    @RequestMapping(value = "treeData", produces = "application/json; charset=UTF-8")
    public List<Map<String, Object>> treeData(@RequestParam(required=false) Long extId, @RequestParam(required=false) String call, HttpServletResponse response) {
        response.setContentType("application/json; charset=UTF-8");
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<Menu> list = platformService.findAllMenu();
        for (Menu e : list) {
            if (extId == null || !extId.equals(e.getId()) && !e.getParentIds().contains("," + extId + ",")) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParent() != null ? e.getParent().getId() : 0);
                map.put("name", e.getName());
                mapList.add(map);
            }
        }

        return mapList;

    }

}
